﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnTheWay.Interfaces;
using OnTheWay.Models;

namespace OnTheWay.Domain
{
    public class Domain<T, TIService> : DomainBase<T, TIService>, IObjectBehaviors<T, Guid>
    {
        public bool Insert(Guid authid, T model)
        {
            if (_isDebug)
            {
                return true;
            }
            object obj = this.Invoke("Insert", new object[] { authid,model });
            return bool.Parse(obj + "");
        }

        public bool Update(Guid authid, T model)
        {
            if (_isDebug)
            {
                return true;
            }
            object obj = this.Invoke("Update", new object[] { authid, model });
            return bool.Parse(obj + "");
        }

        public bool Delete(Guid authid, T model)
        {
            if (_isDebug)
            {
                return true;
            }

            object obj = this.Invoke("Delete", new object[] { authid, model });
            return bool.Parse(obj + "");
        }

        public T GetByID(Guid authid, Guid id)
        {
            if (_isDebug)
            {
                return (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]);
            }

            object obj = this.Invoke("GetByID", new object[] { authid, id });
            return (T)obj;
        }

        public bool UpdateList(Guid authid, IList<T> list)
        {
            if (_isDebug)
            {
                return true;
            }

            object obj = this.Invoke("UpdateList", new object[] { authid, list });
            return bool.Parse(obj + "");
        }

        public bool Exists(Guid authid, Guid id)
        {
            if (_isDebug)
            {
                return true;
            }

            object obj = this.Invoke("Exists", new object[] { authid, id });
            return bool.Parse(obj + "");
        }

        public bool DeleteList(Guid authid, IList<T> list)
        {
            if (_isDebug)
            {
                return true;
            }

            object obj = this.Invoke("DeleteList", new object[] { authid, list });
            return bool.Parse(obj + "");
        }

        public bool DeleteByID(Guid authid, Guid id)
        {
            if (_isDebug)
            {
                return true;
            }

            object obj = this.Invoke("DeleteByID", new object[] { authid, id });
            return bool.Parse(obj + "");
        }

        public long GetRecordCount(Guid authid, string where)
        {
            if (_isDebug)
            {
                return 0;
            }

            object obj = this.Invoke("GetRecordCount", new object[] { authid, where });
            return (long)obj;
        }

        public IList<T> GetListByPage(Guid authid, string where, Page page)
        {
            if (_isDebug)
            {
                return GetList();
            }

            object obj = this.Invoke("GetListByPage", new object[] { authid, where, page });
            return (IList<T>)obj;
        }

        public IList<T> GetListWhere(Guid authid, string where, string order, string asc)
        {
            if (_isDebug)
            {
                return GetList();
            }

            object obj = this.Invoke("GetListWhere", new object[] { authid, where, order, asc });
            return (IList<T>)obj;
        }

        public IList<T> GetInValidData(Guid authid)
        {
            if (_isDebug)
            {
                return GetList();
            }

            object obj = this.Invoke("GetInValidData", new object[] { authid });
            return (IList<T>)obj;
        }

        public IList<T> GetInValidDataPage(Guid authid,Page page)
        {
            if (_isDebug)
            {
                return GetList();
            }

            object obj = this.Invoke("GetInValidDataPage", new object[] { authid, page });
            return (IList<T>)obj;
        }

        private List<T> GetList()
        {
            try
            {
                List<T> list = new List<T>()
                {
                    (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]),
                    (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]),
                    (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]),
                    (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]),
                };

                int i = 1;
                foreach (var item in list)
                {
                    try
                    {
                        typeof(T).GetProperty("Name").SetValue(item, "测试数据"+i++, null);
                    }
                    catch (Exception)
                    {
                    }
                }

                return list;
            }
            catch (Exception)
            {
                return new List<T>();
            }
        }

        private T GetModel()
        {
            try
            {
                T t = (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]);
                typeof(T).GetProperty("Name").SetValue(t, "测试数据1", null);
                return t;
            }
            catch (Exception)
            {
                return default(T);
            }
        }
    }
}
